/**
 * There are <a href="https://www.hdzhx.com">邯郸智讯</a> code generation
 */
package com.boxin.ims.modules.wechat.score.entity;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.validator.constraints.Length;

import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.thinkgem.jeesite.common.persistence.BaseEntity;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * 积分对换记录Entity
 * @author Jakemanse
 * @version 2013-09-23
 */
@Entity
@Table(name = "wp_exchange_record")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ExchangeRecord extends BaseEntity {
	
	public final static String STATUS_DROPPED = "已作废";
	public final static String STATUS_EXCHANGED = "已兑换";
	public final static String STATUS_NO_EXCHANGED = "未兑换";
	
	
	private static final long serialVersionUID = 1L;
	private Long id; 		// 编号
	private User user; 		// 用户
	private WechatMember member;	//微信会员
	private ScoreExchange exchange;	//对换的奖品
	private QRCode qrCode ;		//二维码对象
	private Integer costScore;	//花费的积分
	private String status;		//状态 0：初始未兑换  1：已兑换   2. 已作废
	private String name; 	// 名称
	private Date createTime;	// 创建日期
	private Date updateTime;	//更新时间
	private Date endTime;			//过期时间
	private String delFlag;	// 删除标记（0：正常；1：删除）
	private String remarks; // 备注
	
	private boolean valid;
	@Transient
	private Date startTime;	//Query

	public ExchangeRecord() {
		this.createTime = new Date();
		this.delFlag = DEL_FLAG_NORMAL;
	}

	public ExchangeRecord(Long id){
		this();
		this.id = id;
	}
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	//@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_score_exchangeRecord")
	//@SequenceGenerator(name = "seq_score_exchangeRecord", sequenceName = "seq_score_exchangeRecord")
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@ManyToOne
	@JoinColumn(name="user_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@NotNull
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	@Length(min=1, max=200)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Length(min=0, max=255)
	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
	
	@NotNull
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Length(min=1, max=1)
	public String getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(String delFlag) {
		this.delFlag = delFlag;
	}

	@ManyToOne( fetch=FetchType.LAZY)
	@JoinColumn(name="member_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@NotNull
	public WechatMember getMember() {
		return member;
	}

	public void setMember(WechatMember member) {
		this.member = member;
	}

	@ManyToOne
	@JoinColumn(name="exchange_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@NotNull
	public ScoreExchange getExchange() {
		return exchange;
	}

	public void setExchange(ScoreExchange exchange) {
		this.exchange = exchange;
	}

	public Integer getCostScore() {
		return costScore;
	}

	public void setCostScore(Integer costScore) {
		this.costScore = costScore;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
	
	
	@Transient
	public boolean isValid(){
		java.util.Calendar c1=java.util.Calendar.getInstance();
		java.util.Calendar c2=java.util.Calendar.getInstance();
		try {
			c1.setTime(new Date());
			c2.setTime(endTime);
		} catch (Exception e) {
			System.err.println("格式不正确");
			return true;
		}
		int result = c1.compareTo(c2);
		if (result > 0){	//c1>c2  过期
			return false;
		}
		
//		Date nowDate = new Date();
//		if(nowDate.getTime() > endTime.getTime()){
//			return false;
//		}
		return true;
	}

	public void setValid(boolean valid) {
		this.valid = valid;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	
	@ManyToOne
	@JoinColumn(name="qrcode_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@NotNull
	public QRCode getQrCode() {
		return qrCode;
	}

	public void setQrCode(QRCode qrCode) {
		this.qrCode = qrCode;
	}

	@Transient
	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}
	
	
	
	
}


